package cc.SSM.utils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by rongmc on 16/8/9.
 */
public class XmlUtil {


    public static SortedMap<String,String> parseXml(String  xmlStr)
            throws Exception {
        // 解析结果存储在SortedMap
        SortedMap<String,String> map = new TreeMap<String,String>();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = (Document) reader.read(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
        // 得到xml根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();

        // 遍历所有子节点
        for (Element e : elementList)
            map.put(e.getName(), e.getText());

        return map;
    }

}
